package com.example.pei.ithinklearnt.view.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.example.pei.ithinklearnt.AppApplication;
import com.example.pei.ithinklearnt.R;
import com.example.pei.ithinklearnt.adapter.NewCommentAdapter;
import com.example.pei.ithinklearnt.adapter.base.OnRecyclerViewListener;
import com.example.pei.ithinklearnt.bean.AlumniCircle;
import com.example.pei.ithinklearnt.bean.Comment;
import com.example.pei.ithinklearnt.db.NewComment;
import com.example.pei.ithinklearnt.db.dao.CommentDaoOrder;
import com.example.pei.ithinklearnt.model.AlumniCircleModel;
import com.example.pei.ithinklearnt.model.AlumniCircleModelImp;
import com.example.pei.ithinklearnt.view.base.BaseActivity;
import com.example.pei.ithinklearnt.view.base.IMutlipleItem;
import com.kaopiz.kprogresshud.KProgressHUD;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.bmob.v3.exception.BmobException;

import static com.example.pei.ithinklearnt.utils.BeanOperationUtil.putBean;

public class NewCommentActivity extends BaseActivity {

    private ArrayList<NewComment> allNewComments;
    private AlumniCircleModel alumniCircleModel=new AlumniCircleModelImp();
    private KProgressHUD hud; //等待进度条

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;


    @OnClick(R.id.button_backward)
    void back(){
        finish();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_comment);

        //查找所有评论
        allNewComments=CommentDaoOrder.getInstance(this).loadAllNewComments();

        initUI();

    }


    private void initUI(){
        IMutlipleItem<NewComment> mutlipleItem= new IMutlipleItem<NewComment>() {
            @Override
            public int getItemLayoutId(int viewtype) {
                return R.layout.item_new_comment;
            }

            @Override
            public int getItemViewType(int postion, NewComment comment) {
                return 0;
            }

            @Override
            public int getItemCount(List<NewComment> list) {
                return allNewComments!=null?allNewComments.size():0;
            }
        };

        NewCommentAdapter adapter=new NewCommentAdapter(this,mutlipleItem,allNewComments);
        adapter.setOnRecyclerViewListener(new OnRecyclerViewListener() {
            @Override
            public void onItemClick(View v, final int position) {
                hud = KProgressHUD.create(NewCommentActivity.this)
                        .setStyle(KProgressHUD.Style.SPIN_INDETERMINATE)
                        .setDimAmount(0.4f)
                        .setLabel("加载中...")
                        .setCornerRadius(10)
                        .show();

                alumniCircleModel.findAlumniCircleByCommentId(allNewComments.get(position).getId(), new AlumniCircleModelImp.findAlumniCircle() {
                    @Override
                    public void success(AlumniCircle object) {
                        if(object!=null){
                            //启动详情页活动
                            Intent intent=new Intent(NewCommentActivity.this,DynamicDetails.class);
                            //将动态存到本地，方便等会详情页获取数据
                            putBean(NewCommentActivity.this,"alu","1",object);

                            intent.putExtra("permission",getPermission(position,object));            //能操作的权限

                            startActivity(intent);
                            hud.dismiss();
                        }
                    }

                    @Override
                    public void failed(BmobException e) {
                        hud.dismiss();
                    }
                });



            }

            @Override
            public boolean onItemLongClick(View v, int position) {
                return false;
            }
        });
        recyclerView.setAdapter(adapter);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        //adapter.bindDatas(data);


    }

    //返回为所选的item创建dialog时的权限
    private int getPermission(int position,AlumniCircle object){
        int permission=0;
        if(AppApplication.getCurrentUser()!=null){
            String username=AppApplication.getCurrentUser().getUsername();
            String senderName=object.getSender().getUsername();  //发送者姓名
            int userType=AppApplication.getCurrentUser().getUserType();
            //如果是自己发送的或者是管理员 则可以删除
            if(senderName.equals(username)&&userType==1){
                //是自己发送的，且不是管理员,普通用户
                permission=1;
            } else if(senderName.equals(username)&&userType==0){
                //是自己发送的，且是管理员
                permission=2;
            }else if(!senderName.equals(username)&&userType==0){
                //不是自己发送的，但是是管理员
                permission=3;
            }else if(!senderName.equals(username)&&userType==1){
                //不是自己发送的，且不是管理员
                permission=0;
            }
        }
        return permission;
    }


    @Override
    protected void onPause() {
        //更新所有评论未读为已读
        CommentDaoOrder.getInstance(this).updateBatchStatusReaded();

        super.onPause();
    }
}

